#######################
# Figure 1
# This uses WTGN's original data
# but plots point for all respondents
#######################

#Group responses to justified, by engaged/disengaged, partisan id in WGTN Study 1
plot1ajustifieddata <- study1 %>%
  group_by(alignment,passed) %>%
  summarise(smean = mean(justified, na.rm = TRUE),
            ssd = sd(justified, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower = lower_ci(smean, se, count),
         upper = upper_ci(smean, se, count))

#Group responses to justified, by partisan id in WGTN Study 1
plot1ajustifieddata.tot <- study1 %>%
  group_by(alignment) %>%
  summarise(smean = mean(justified, na.rm = TRUE),
            ssd = sd(justified, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower = lower_ci(smean, se, count),
         upper = upper_ci(smean, se, count))

#add column of "All Respondents" to make this fit with conditional on engaged/disengaged
plot1ajustifieddata.tot<-add_column(plot1ajustifieddata.tot,passed=rep("All Respondents",3),.after = 1)

#Group responses to justified, by engaged/disengaged, partisan id in WGTN Study 2
plot1bjustifieddata <- study2 %>%
	group_by(alignment, passed) %>%
	summarise(smean = mean(justified, na.rm = TRUE),
			  ssd = sd(justified, na.rm = TRUE),
			  count = n()) %>%
	mutate(se = ssd / sqrt(count),
		   lower = lower_ci(smean, se, count),
		   upper = upper_ci(smean, se, count))

#Group responses to justified, by partisan id in WGTN Study 2
plot1bjustifieddata.tot <- study2 %>%
  group_by(alignment) %>%
  summarise(smean = mean(justified, na.rm = TRUE),
            ssd = sd(justified, na.rm = TRUE),
            count = n()) %>%
  mutate(se = ssd / sqrt(count),
         lower = lower_ci(smean, se, count),
         upper = upper_ci(smean, se, count))

#Add column to hold condition of "all respondents"
plot1bjustifieddata.tot<-add_column(plot1bjustifieddata.tot,passed=rep("All Respondents",3),.after = 1)

#Group responses to justified, by engaged/disengaged, partisan id in WGTN Study 3
plot1cjustifieddata <- study3 %>%
	group_by(alignment, passed) %>%
  summarise(smean = weighted.mean(justified, w=weight, na.rm = TRUE),
            ssd = sd(justified, na.rm = TRUE),
            count = n(),
            se=ssd/sqrt(count),
            lower = weighted.ttest.ci(justified, weights=weight)[1],
            upper = weighted.ttest.ci(justified, weights=weight)[2])

#Group responses to justified, by partisan id in WGTN Study 3
plot1cjustifieddata.tot <- study3 %>%
  group_by(alignment) %>%
  summarise(smean = weighted.mean(justified, w=weight, na.rm = TRUE),
            ssd = sd(justified, na.rm = TRUE),
            count = n(),
            se=ssd/sqrt(count),
            lower = weighted.ttest.ci(justified, weights=weight)[1],
            upper = weighted.ttest.ci(justified, weights=weight)[2])

#Add column to hold condition of "all respondents"
plot1cjustifieddata.tot<-add_column(plot1cjustifieddata.tot,passed=rep("All Respondents",2),.after = 1)

#Merge all values together for plotting
plot1justifiedmerged <- bind_rows(plot1cjustifieddata.tot,
                                  plot1cjustifieddata,
                                  plot1bjustifieddata.tot,
                                  plot1bjustifieddata,
                                  plot1ajustifieddata.tot, 
                                  plot1ajustifieddata
                                  )

#Re-label the categories for Study 2
#This is just making the labels match those in WGTN
levels(plot1justifiedmerged$alignment)[3]<-c("Apolitical Shooter")
levels(plot1justifiedmerged$alignment)[4]<-c("Out-Party Shooter")
levels(plot1justifiedmerged$alignment)[5]<-c("In-Party Shooter")


#Reorder the data to match order in WGTN
plot1justifiedmerged$alignment <- fct_relevel(plot1justifiedmerged$alignment,  " In-Party Shooter"," Out-Party Shooter","In-Party Shooter","Out-Party Shooter","Apolitical Shooter", "In-Party Driver", "Out-Party Driver","Apolitical Driver")

#Plot the data
plot1justified <- ggplot(plot1justifiedmerged, aes(x = smean, y = alignment, color=passed,shape=passed))+ 
  xlim(-0.01,1)+
  scale_y_discrete(c())+
	ggtitle("Suspect is Justified") +	
  geom_point(size = 2) +
  scale_colour_hue(l=40)+
  theme_bw() +	
  theme(legend.title=element_blank())+
  theme(plot.title = element_text(hjust = 0.5))+
  geom_errorbarh(height = 0, aes(xmin = lower, xmax = upper),show.legend=FALSE)+
  geom_hline(yintercept=c(1.5,2.5, 3.5, 4.5, 5.5, 6.5, 7.5), linetype="dotted") + 
	geom_hline(yintercept=c(2.5, 5.5), linetype="solid", size=2) +
  xlab("Proportion Saying Suspect is Justified (Lines represent 95% CI)")+
  ylab(" ")+
	annotate("text", x = .8, y=8, label = "Study 1\n(Qualtrics)",  size=3) +
	annotate("text", x = .8, y=5, label = "Study 2\n(Qualtrics)",  size=3) +
	annotate("text", x = .8, y=2, label = "Study 3\n(YouGov)", size=3) 

#save output to a tiff file
ggsave("../Main Results/Fig1.tif", width=6, height=6, units = "in",dpi=600)





